package com.danger.Presenter;

import android.content.Context;

import com.danger.Model.action.AddMyObjectInfoModel;
import com.danger.Model.action.AddMyObjectInfoModelImpl;
import com.danger.Model.bean.ObjectInfo;
import com.danger.Model.listener.OnAddListener;
import com.danger.Model.listener.OnUploadPhotoListener;
import com.danger.View.AddMyObjectInfoView;

/**
 * Created by danger on 2016/3/30.
 */
public class AddMyObjectInfoPresenterImpl implements AddMyObjectInfoPresenter {

    private Context context;
    private AddMyObjectInfoModel addMyObjectInfoModel;
    private AddMyObjectInfoView addMyObjectInfoView;

    public AddMyObjectInfoPresenterImpl(Context context, AddMyObjectInfoView addMyView) {
        this.context = context;
        this.addMyObjectInfoView = addMyView;
        addMyObjectInfoModel = new AddMyObjectInfoModelImpl(context);

    }

    @Override
    public void addObjectInfo() {
        ObjectInfo lost = addMyObjectInfoView.getObjectInfo();
        String result = addMyObjectInfoModel.check(lost);
        if (result.equals("正确")) {
            addMyObjectInfoView.showDialog();
            addMyObjectInfoModel.addMyObjectInfo(lost, new OnAddListener() {
                @Override
                public void onSuccess() {
                    addMyObjectInfoView.hideDialog();
                    addMyObjectInfoView.showSuccess("添加失物成功", null);
                    addMyObjectInfoView.back();
                }

                @Override
                public void onError(String s) {
                    addMyObjectInfoView.hideDialog();
                    addMyObjectInfoView.showError(s);
                }
            });
        } else {
            addMyObjectInfoView.showError(result);
        }
    }

    @Override
    public void uploadPhoto(String path) {
        addMyObjectInfoView.showDialog();
        addMyObjectInfoModel.uploadPhoto(path, new OnUploadPhotoListener() {
            @Override
            public void onSuccess(String url) {
                addMyObjectInfoView.hideDialog();
                addMyObjectInfoView.showSuccess("上传成功", url);
            }

            @Override
            public void onError(String s) {
                addMyObjectInfoView.hideDialog();
                addMyObjectInfoView.showError(s);
            }
        });
    }
}
